package com.helloandroid.paso.ui.concert;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.helloandroid.paso.R;

public class Concert extends AsyncTask<String, Void, Void> {
		
	private WebView webView;
	private ProgressBar pb;
	

	/** 
	 * Usage:
	 *  	getConcertsView(
	 *  		getApplicationContext(),
	 *  		(WebView) findViewById(R.id.concert_webview),
	 *  		(ProgressBar) findViewById(R.id.pbLoading)
	 *  	);
	 *  	
	 *  
	 */
	
	
	public void getConcertsView(final Context context, WebView webview, final ProgressBar pb) {

		this.webView = webview;
		this.pb = pb;
		webview.setBackgroundColor(R.color.paso_light);		
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
            	pb.setVisibility(View.INVISIBLE);
               
            	Toast.makeText(context, "No internet connection", Toast.LENGTH_LONG).show();
            	
            }
            public void onPageFinished(WebView view, String url){
            	pb.setVisibility(View.GONE);
            	webView.setEnabled(true);
            }
            
            
            public void onPageStarted(WebView view, String url, Bitmap favicon){
            	pb.setVisibility(View.VISIBLE);
            	webView.setEnabled(false);

            }
            
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {

                view.loadUrl(url);
                return true;
            }
        });

        loadConcertUrl(context.getString(R.string.allConcertsUrl));
	}

	public void loadConcertUrl(String url){
		String urls[] = {url};
		doInBackground(urls);
	}

	@Override
	protected Void doInBackground(String... urls) {
			pb.setVisibility(View.VISIBLE);
			webView.setEnabled(false);
			webView.loadUrl(urls[0]);
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		webView.setEnabled(true);
		pb.setVisibility(View.GONE);

	}

    

}
